package cn.jane.bigevent.controller;

import cn.jane.bigevent.entity.Article;
import cn.jane.bigevent.entity.PageBean;
import cn.jane.bigevent.entity.Result;
import cn.jane.bigevent.service.ArticleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文章模块
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    private final ArticleService articleService;

    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 新增文章
     */
    @PostMapping
    public Result<Object> add(@Validated(Article.Add.class) @RequestBody Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * 分页查询文章
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum, Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state) {
        return Result.success(articleService.list(pageNum, pageSize, categoryId, state));
    }

    /**
     * 查询文章详情
     */
    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id) {
        return Result.success(articleService.findById(id));
    }

    /**
     * 更新文章
     */
    @PutMapping
    public Result<Object> update(@Validated(Article.Update.class) @RequestBody Article article) {
        articleService.update(article);
        return Result.success();
    }

    /**
     * 删除文章
     */
    @DeleteMapping
    public Result<Object> delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
}
